<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')" :align-center="true">
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="130px" label-position="right">
			<el-form-item label="检验项目编号" prop="code">
				<el-input v-model="form.code" placeholder="请输入检验项目编号" clearable></el-input>
			</el-form-item>
			<el-form-item label="检验项目名称" prop="name">
				<el-input v-model="form.name" placeholder="请输入检验项目名称" clearable></el-input>
			</el-form-item>
            <el-form-item label="检验项目分组" prop="group">
                <el-select v-model="form.group" placeholder="请选择分类" style="width:100%" clearable filterable>
                    <el-option v-for="item in cateList" :key="item.id" :label="item.group_name" :value="item.id"/>
                </el-select>
			</el-form-item>
            <el-form-item label="检验方法" prop="method">
				<!-- <el-input v-model="form.method" placeholder="请输入检验方法" clearable></el-input> -->
                <el-select v-model="form.method" placeholder="请选择检验方法" style="width:100%" clearable @change="methodChange">
                    <el-option label="定性" value="定性"/>
                    <el-option label="定量" value="定量"/>
                </el-select>
			</el-form-item>
            <el-form-item label="检验标准" prop="standard">
				<el-input v-model="form.standard" placeholder="请输入检验标准" clearable></el-input>
			</el-form-item>
            <el-form-item label="单位" prop="unit">
				<el-input v-model="form.unit" placeholder="请输入单位" clearable :disabled="form.method == '定性'"></el-input>
			</el-form-item>
            <el-form-item label="标准值" prop="stvalue">
				<el-input v-model="form.stvalue" placeholder="请输入标准值" clearable :disabled="form.method == '定性'"></el-input>
			</el-form-item>
            <el-form-item label="检验上限" prop="upper">
				<el-input v-model="form.upper" placeholder="请输入检验上限" clearable :disabled="form.method == '定性'"></el-input>
			</el-form-item>
            <el-form-item label="检验下限" prop="lower">
				<el-input v-model="form.lower" placeholder="请输入检验下限" clearable :disabled="form.method == '定性'"></el-input>
			</el-form-item>
            <el-form-item label="备注" prop="note">
				<el-input v-model="form.note" placeholder="请输入备注" clearable type="textarea"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>

export default {
	emits: ['success', 'closed'],
	components: {

	},
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增',
				edit:'编辑',
				show:'查看'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
                id: "",
				code: "",
				name: "",
				group: "",
				standard: "",
				method: "",
				unit: "",
				stvalue: "",
				upper: "",
                lower: "",
				note: "",
			},
			//验证规则
			rules: {
				end_price: [
					{ required: true, message: '请输入尾款' }
				],
			},
            cateList: []
		}
	},
	mounted() {
        this.getCate()
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
            let sub = true
            if(this.form.method == '定量'){
                if(this.form.stvalue != '' || this.form.upper != '' || this.form.lower != ''){
                    sub = true
                }else{
                    this.$message.error('标准值,检验上限,检验下限必须填写一项')
                    sub = false
                }
            }
            if(sub){
                this.$refs.dialogForm.validate(async (valid) => {
                    if (valid) {
                        this.isSaveing = true;
                        var res = [];
                        if (this.form.id) {
                            res = await this.$API.production.item.edit.post(this.form);
                        } else {
                            res = await this.$API.production.item.add.post(this.form);
                        }
                        this.isSaveing = false;
                        if (res.code == 1) {
                            this.$emit('success', this.form, this.mode)
                            this.visible = false;
                            this.$message.success("操作成功")
                        } else {
                            this.$alert(res.message, "提示", { type: 'error' })
                        }
                    } else {
                        return false;
                    }
                })
            }
		},
        async getCate(){
			var res = await this.$API.production.itemType.list.get();
			this.cateList = res.data
		},
		//表单注入数据
		setData(data) {
			this.form.id = data.id
            this.form.code = data.code
            this.form.name = data.name
            this.form.group = data.group
            this.form.standard = data.standard
            this.form.method = data.method
            this.form.unit = data.unit
            this.form.stvalue = data.stvalue
            this.form.upper = data.upper
            this.form.lower = data.lower
            this.form.note = data.note
		},
        methodChange(e){
            if(e == '定量'){
                this.rules.unit = [
                    { required: true, message: '请输入单位', trigger: 'change' },
                ]
            }else {
                this.rules.unit = []
            }
        },
	}
}
</script>

<style lang="scss">
</style>
